import VueRouter from "vue-router";
import About from "../pages/About";
import Home from "../pages/Home";
import News from "../pages/News";
import Message from "../pages/Message";
import Detail from "../pages/Detail";
const router = new VueRouter({
  routes: [
    {
      path: "/about",
      component: About,
      meta: { title: "关于" },
    },
    {
      path: "/home",
      component: Home,
      meta: { title: "主页" },
      children: [
        {
          path: "news",
          component: News,
          meta: { title: "新闻" },
        },
        {
          path: "message",
          component: Message,
          meta: { title: "消息" },
          children: [
            {
              name: "xiangqing",
              path: "detail",
              component: Detail,
              // props的第一种写法，值为对象,该对象中的所有key-value都会以props的形式传给Detail组件
              // props: { a: 1, b: 'hello' }
              // props的第二种写法，值为布尔值,布尔值为真，就会把路由组件收到的所有params参数，以props的形式传给Detail参数
              //   props: true,
              // props的disanzhongxiefa，值为函数、
              //   props($route) {
              //     return { id: $route.query.id, title: $route.query.title };
              //   },

              // // params参数
              // path: 'detail/:id/:title',
              meta: { isAuth: true, title: "详情" },
            },
          ],
        },
      ],
    },
  ],
});

// 全局前置路由守卫--每次路由切换之前，初始化的时候被调用
router.beforeEach((to, from, next) => {
  console.log("路由前置守卫", to, from);

  // if(to.name === 'xinwen' || to.name === 'xiaoxi')
  // if (to.path === '/home/news' || to.path === '/home/message') {
  if (to.meta.isAuth) {
    if (to.path === "/home/message/detail") {
      next();
    } else {
      alert("学校名不对，无权限查看");
    }
  } else {
    next();
  }
});

router.afterEach((to, from) => {
  console.log("后置路由守卫", to, from);
  document.title = to.meta.title || "主页";
});

export default router;
